﻿using B.Z.BasicData.Domain.Entity.Authorize;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.Z.BasicData.API.Read.Application.Role
{
    public class RoleQueryCommandHandler : IRequestHandler<RoleQueryCommand, APIResult<APIPaging<T_Role>>>
    {
        private readonly IBaseRepository<T_Role> roleRepository;

        public RoleQueryCommandHandler(IBaseRepository<T_Role> roleRepository)
        {
            this.roleRepository = roleRepository;
        }

        public Task<APIResult<APIPaging<T_Role>>> Handle(RoleQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<T_Role>> result = new APIResult<APIPaging<T_Role>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            var list = roleRepository.GetAll().AsNoTracking();
            APIPaging<T_Role> paging = new APIPaging<T_Role>();
            paging.RecordCount = list.Count();
            paging.List = list.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
